package com.hr.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hr.model.UserModel;
import com.hr.model.WageModel;
import com.hr.service.WageService;
/**
 * 工资控制类
 * @author fazhen.zheng
 *
 */
@Controller
public class WageController {
	@Autowired
	private WageService wageService;
	/**
	 * 获得工资列表
	 * @return
	 */
	@RequestMapping("/wage/wageList")
	public ModelAndView wageList(){
		ModelAndView mv = new ModelAndView("wage/wageList");
		List wageList = wageService.findAll();
		mv.addObject("wages", wageList);
		return mv;
		
	}
	
	@RequestMapping("/wage/prepareEditWage")
	public ModelAndView prepareEditWage(){
		ModelAndView mv = new ModelAndView("wage/editWage");
		return mv;
	}
	/**
	 * 编辑工资
	 * @param wageModel
	 * @return
	 */
	@RequestMapping("/wage/editWage")
	public ModelAndView EditWage(WageModel wageModel){
		wageService.editWage(wageModel);
		return wageList();
	}
	/**
	 * 删除薪资
	 * @param user_id
	 * @param wage_date
	 * @return
	 */
	@RequestMapping("/wage/deleteWage")
	public ModelAndView deleteWage(String user_id,String wage_date){
		wageService.deleteWage(user_id, wage_date);
		return wageList();
	}
	/**
	 * 检查工资是否已发放
	 * @param user_id
	 * @param wage_date
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/wage/checkWageHaveSend")
	public void checkWageHaveSend(String user_id,String wage_date,HttpServletRequest request,
			HttpServletResponse response) throws IOException{
		String result = wageService.checkWageHaveSend(user_id, wage_date);
		response.getWriter().print(result);
		
	}
	@RequestMapping("/wage/userWage")
	public ModelAndView userWage(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession();
		UserModel user = (UserModel) session.getAttribute("user");
		ModelAndView mv = new ModelAndView("wage/wageList");
		List wageList = wageService.findByUserId(user.getUser_id());
		mv.addObject("wages", wageList);
		mv.addObject("userQuery", true);
		return mv;
	}
}
